import os
import shutil
import paddlehub as hub
from utils.enlarge_eyes import enlarge_eyes
from utils.rouge import rouge
from utils.whitening import whitening
from utils.thin_face import thin_face
from utils.mix_picture import mix_picture
import numpy as np
import io
import cv2

def convert_str_to_RGB(rgb_str):
    rgb_int = int(rgb_str[1:], 16)
    r = rgb_int & int('FF', 16)
    g = (rgb_int & int('FF00', 16)) >> 8
    b = (rgb_int & int('FF0000', 16)) >> 16
    return [r, g, b]

def get_face_img(form, input_imgs):
    face_landmark_module = hub.Module(name="face_landmark_localization")
    result = face_landmark_module.keypoint_detection(images=input_imgs)
    # 美颜功能
    for index, r in enumerate(input_imgs):
        face_landmark = np.array(result[index]['data'][0], dtype='int')
        if form.get('thinFaceChecked') == 'true':
            input_imgs[index] = thin_face(input_imgs[index], face_landmark)
        if form.get('bigEyeChecked') == 'true':
            enlarge_eyes(input_imgs[index], face_landmark, radius=13, strength=13)
        if form.get('redMouseChecked') == 'true':
            rouge(input_imgs[index], face_landmark)
        if form.get('whiteChecked') == 'true':
            whitening(input_imgs[index], face_landmark)

    deeplab_module = hub.Module(name="deeplabv3p_xception65_humanseg")
    masks = deeplab_module.segmentation(images=input_imgs)

    for index, img in enumerate(input_imgs):
        # cv2.imwrite('2.jpg', img)
        mix_picture(convert_str_to_RGB(form.get('color')), img, masks[index])
        # cv2.imwrite('3.jpg', img)